package com.agent.platform.controller;

import com.agent.platform.common.PageResult;
import com.agent.platform.common.Result;
import com.agent.platform.dto.TaskCallbackDTO;
import com.agent.platform.dto.TaskCreateDTO;
import com.agent.platform.service.TaskService;
import com.agent.platform.vo.TaskVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/task")
@RequiredArgsConstructor
public class TaskController {
    
    private final TaskService taskService;
    
    @PostMapping("/create")
    public Result<TaskVO> create(@Valid @RequestBody TaskCreateDTO dto) {
        return Result.success(taskService.create(dto));
    }
    
    @PostMapping("/callback")
    public Result<Void> callback(@Valid @RequestBody TaskCallbackDTO dto) {
        taskService.callback(dto);
        return Result.success();
    }
    
    @GetMapping("/list")
    public Result<PageResult<TaskVO>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String taskType,
            @RequestParam(required = false) String status) {
        Page<TaskVO> page = taskService.list(current, size, taskType, status);
        PageResult<TaskVO> result = new PageResult<>(
            page.getTotal(), 
            page.getRecords(), 
            page.getCurrent(), 
            page.getSize()
        );
        return Result.success(result);
    }
    
    @GetMapping("/{taskId}")
    public Result<TaskVO> getDetail(@PathVariable String taskId) {
        return Result.success(taskService.getDetail(taskId));
    }
}
